# -*- coding: utf-8 -*-

from flask.ext.restful import Resource, abort
from spider.site_index import BaiduSiteIndex, GoogleSiteIndex, BingSiteIndex,\
        SogouSiteIndex, SoSiteIndex
from PalmWebsite.api import parser, request_cache
from PalmWebsite.model import Site, SiteSE
import utils
from datetime import datetime, date
from PalmWebsite.extensions import db

class Greeting(Resource):

    def get(self):
        return {
                "greeting": "hello",
                "success": True
            }


class Index(Resource):
    SE = {
          "baidu": BaiduSiteIndex,
          "google": GoogleSiteIndex,
          "bing": BingSiteIndex,
          "sogou": SogouSiteIndex,
          "so": SoSiteIndex
        }

    #@request_cache
    def get(self, se):
        if se not in self.SE:
            abort(404, err_code=20000, success=False)
        args = parser.parse_args()
        domain = args.get("domain")
        if not domain:
            abort(400, err_code=10000, success=False)

        site = Site.query.filter_by(domain=utils.get_host(domain)).first()

        if not site:
            se_index = self.SE[se](utils.get_host(domain))
            data = se_index.get_nums()

            return {
                    "success": True,
                    "data": {
                        "domain": domain,
                        "index_number": data
                    }
                }

        site_se = SiteSE.query.filter_by(site_id=site.id).\
                        filter(SiteSE.last_update_date>=date.today()).first()

        if not site_se:
            # 数据库里没有最新的数据，更新
            se_index = self.SE[se](utils.get_host(domain))
            data = se_index.get_nums()
            site_se = SiteSE()
            if se == "baidu":
                site_se.baidu_index = data
            elif se == "so":
                site_se.so_index = data
            elif se == "bing":
                site_se.bing_index = data
            elif se == "google":
                site_se.google_index = data
            elif se == "sogou":
                site_se.sogou_index = data

            site_se.last_update_date = date.today()
            site.site_ses.append(site_se)
            db.session.add(site_se)
            db.session.commit()

        if site_se:
            if se == "baidu":
                data = site_se.baidu_index
            elif se == "so":
                data = site_se.so_index
            elif se == "bing":
                data = site_se.bing_index
            elif se == "google":
                data = site_se.google_index
            elif se == "sogou":
                data = site_se.sogou_index

        if args.get("refresh"):
            # 强制更新数据库里的数据
            se_index = self.SE[se](utils.get_host(site.domain))
            data = se_index.get_nums()
            if se == "baidu":
                site_se.baidu_index = data
            elif se == "so":
                site_se.so_index = data
            elif se == "bing":
                site_se.bing_index = data
            elif se == "google":
                site_se.google_index = data
            elif se == "sogou":
                site_se.sogou_index = data

            site_se.last_update_date = date.today()
            db.session.commit()

        return {
                "success": True,
                "data": {
                    "site_id": site.id,
                    "id": site_se.id,
                    "domain": site.domain,
                    "index_number": data,
                    "last_update_date": utils.date2string(site_se.last_update_date)
                }
            }
